/** */
package co.com.umobile.common.widget;


import java.net.URI;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import co.com.umobile.R;
import co.com.umobile.common.Numbers;

/**
 * @author sergiogranada
 * 
 */
public class ProgressHandler extends Handler {

	/** the progress count. */
	private int progress;

	/** the progress bar associated. */
	private ProgressBar progressBar;

	/** the label view. */
	private TextView label;
	
	/** the status icon.*/
	private ImageView statusIcon;
	
	/** the title text id.*/
	private int titleId;
	
	/** the service Code.*/
	private int serviceCode;
	
	/** the service Uri.*/
	private Uri serviceUri;
	
	
	/** the init message.*/
	private String initMessage;
	
	/** the application context.*/
	private Context context;

	/**
	 * Progress handler.
	 * @param context {@link Context}.
	 * @param progressBar {@link ProgressBar}.
	 * @param label {@link TextView}.
	 * @param statusIcon {@link ImageView} status icon.
	 * @param titleId Title id.
	 * @param serviceCode service code.
	 * @param serviceUri service {@link URI}.
	 */
	public ProgressHandler(Context context, ProgressBar progressBar, TextView label, ImageView statusIcon, int titleId, int serviceCode, Uri serviceUri) {
		this.context = context;
		this.progressBar = progressBar;
		this.label = label;
		this.statusIcon = statusIcon;
		this.titleId = titleId;
		this.serviceCode = serviceCode;
		this.serviceUri = serviceUri;
		String initUpdateMessage = context.getResources().getString(R.string.update_init);
		initMessage = String.format(initUpdateMessage, context.getResources().getString(titleId));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.Handler#handleMessage(android.os.Message)
	 */
	@Override
	public void handleMessage(Message msg) {

		switch (msg.what) {
		case 1:
			setProgress(0);
			if (progressBar != null) {
				getProgressBar().setVisibility(ProgressBar.VISIBLE);
				getProgressBar().setIndeterminate(false);
				getProgressBar().setProgress(0);
				getProgressBar().invalidate();
			}

			if (label != null) {
				label.setText(((String) msg.obj));
			}

			break;

		case 2:
			setProgress(msg.arg1);
			if (progressBar != null) {
				getProgressBar().setProgress(progress);
			}

			if (label != null) {
				label.setText(((String) msg.obj));
			}

			break;

		case Numbers.THREE:
			setProgress(0);
			if (progressBar != null) {
				getProgressBar().setVisibility(ProgressBar.GONE);
				getProgressBar().setProgress(0);
			}

			if (label != null) {
				label.setText(((String) msg.obj));
			}
			
			if (statusIcon != null) {
			  if (msg.arg2 == 1) {
			    getStatusIcon().setImageResource(R.drawable.check_icon);
			  } else if (msg.arg2 == 2) {
	         getStatusIcon().setImageResource(R.drawable.error_icon);
			  }
			  getStatusIcon().setVisibility(ImageView.VISIBLE);
			}
			

			break;
		case Numbers.FOUR:
			if (progressBar != null) {
				getProgressBar().setVisibility(ProgressBar.VISIBLE);
				getProgressBar().setProgress(getProgress());
			}

			if (label != null) {
				label.setText(((String) msg.obj));
			}

			break;
		case Numbers.FIVE:
			if (progressBar != null) {
				getProgressBar().setVisibility(ProgressBar.VISIBLE);
				getProgressBar().setProgress(Numbers.ONE_HUNDRED);
				getProgressBar().setIndeterminate(true);
				getProgressBar().invalidate();
			}

			if (label != null) {
				label.setText(((String) msg.obj));
			}

			break;
		case Numbers.SIX:
			initUpdate();
			break;
		default:
			break;
		}

	}

	/**
	 * @return the progress
	 */
	public int getProgress() {
		return progress;
	}

	/**
	 * @param progress
	 *            the progress to set
	 */
	public void setProgress(int progress) {
		this.progress = progress;
	}

	/**
	 * @return the progressBar
	 */
	public ProgressBar getProgressBar() {
		return progressBar;
	}

	/**
	 * The progressBar to set.
	 * @param progressBarNew progress bar to set.
	 */
	public void setProgressBar(ProgressBar progressBarNew) {

		if (this.progressBar != null) {

			if (progressBar.isIndeterminate()) {
				progressBarNew.setIndeterminate(true);
			} else {
				progressBarNew.setIndeterminate(false);
				progressBarNew.setProgress(progressBar.getProgress());
			}

			progressBarNew.setVisibility(progressBar.getVisibility());

		}

		this.progressBar = progressBarNew;
	}

	/**
	 * @return the label
	 */
	public TextView getLabel() {
		return label;
	}

	/**
	 * @param labelNew new {@link TextView}. 
	 * label label to set.
	 */
	public void setLabel(TextView labelNew) {

		if (label != null) {
			labelNew.setText(label.getText());
		}

		this.label = labelNew;
	}

	
	
	/**
	 * @return the titleId
	 */
	public int getTitleId() {
		return titleId;
	}

	/**
	 * @param titleId the titleId to set
	 */
	public void setTitleId(int titleId) {
		this.titleId = titleId;
	}

	/**
	 * @return the serviceCode
	 */
	public int getServiceCode() {
		return serviceCode;
	}

	/**
	 * @param serviceCode the serviceCode to set
	 */
	public void setServiceCode(int serviceCode) {
		this.serviceCode = serviceCode;
	}

	/**
	 * @return the serviceUri
	 */
	public Uri getServiceUri() {
		return serviceUri;
	}

	/**
	 * @param serviceUri the serviceUri to set
	 */
	public void setServiceUri(Uri serviceUri) {
		this.serviceUri = serviceUri;
	}
	
	/**
	 * sets the init message.
	 */
	public void initMessage() {
		
		if (initMessage != null && label != null) {
			label.setText(initMessage);
		}
		
	}
	
	/**
	 * start the progress bar.
	 */
	public void initProgressBar() {

		if (progressBar != null) {
			progressBar.setVisibility(ProgressBar.VISIBLE);
			progressBar.setProgress(Numbers.ONE_HUNDRED);
			progressBar.setIndeterminate(true);
			progressBar.invalidate();
		}
	}
	
	/**
	 * start the update.
	 */
	public void initUpdate() {
		initMessage();
		initProgressBar();
		
		if (statusIcon != null) {
		  statusIcon.setVisibility(ImageView.GONE);
		}
		
		context.getContentResolver().query(serviceUri, null, null, null, null);
	}

  /**
   * @return the statusIcon
   */
  public ImageView getStatusIcon() {
    return statusIcon;
  }

  /**
   * @param statusIcon the statusIcon to set
   */
  public void setStatusIcon(ImageView statusIcon) {
    this.statusIcon = statusIcon;
  }

}
